package com.hadron.ui.form.comboBox 
{
	import com.hadron.graphics.DrawRectangle;
	import components.events.ButtonEvent;
	import components.templates.AbstractComboBox;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import org.casalib.collection.List;
	/**
	 * ...
	 * @author Hadron
	 */
	public class ComboBox extends AbstractComboBox
	{
		private var _bg:MovieClip = new combo_box_bg_mc();
		private var _comboMainLabel:MovieClip = new combo_box_item_mc();
		private var _comboArrow:MovieClip = new combo_box_arrow_mc();
		private var _mcItems:List = new List();
		private var maxHeight:Number;
		private var bgOriginHeight:Number;
		
		private var itemsContainer:Sprite = new Sprite();
		
		public function ComboBox() 
		{
			super();
			init();
		}
		
		private function init():void
		{
			maxHeight = bgOriginHeight = _bg.height;
			_bg.mouseChildren = false;
			addChild(_bg);
			
			_comboMainLabel.x = 8;
			_comboMainLabel.y = 3;
			_comboMainLabel.mouseChildren = false;
			_comboMainLabel.bg.alpha = 0;
			addChild(_comboMainLabel);
			
			_comboArrow.x = (_bg.width - _comboArrow.width) - 5; //4 - stroke size
			_comboArrow.y = 2;
			_comboArrow.mouseChildren = false;
			addChild(_comboArrow);
			
			itemsContainer.y = bgOriginHeight;
		}
		
		override public function enable():void
		{
			super.enable();
			//add event listeners to arrow button
			_comboArrow.addEventListener(MouseEvent.CLICK, handleArrowClick, false, 0, true);
			_comboArrow.buttonMode = true;
		}
		
		private function handleArrowClick(e:MouseEvent):void 
		{
			blockArrow();
			(isOpened) ? close() : open();
		}
		
		override public function open():void
		{
			isOpened = true;
			TweenMax.to(_comboArrow.arrow, .5, { rotation:180, ease:Quart.easeOut} );
			TweenMax.to(_bg, .5, { scaleY:1 + (maxHeight / bgOriginHeight), ease:Quart.easeInOut } );
			TweenMax.delayedCall(.2, addContainer);
		}
		
		override public function close():void
		{
			isOpened = false;
			TweenMax.to(_comboArrow.arrow, .5, { rotation:0, ease:Quart.easeOut} );
			TweenMax.to(_bg, .5, { scaleY:1, delay:.15, ease:Quart.easeInOut } );
			removeContainer()
		}
		
		private function addContainer():void
		{
			addChild(itemsContainer);
			enableItems();
		}
		
		private function removeContainer():void
		{
			disableItems();
			TweenMax.delayedCall((_mcItems.size) * .1, removeChild, [itemsContainer]);
		}
		
		private function enableItems():void
		{
			for (var i:int = 0; i < _mcItems.size; i++) 
			{				
				TweenMax.delayedCall(.05 * i, (_mcItems.getItemAt(i) as PhotoCenterComboBoxItem).transitionIn);
			}
			
			//unblock arrow when animation finishes - last element transitionIn is Complete
			TweenMax.delayedCall((.05 * _mcItems.size) + .3, unblockArrow);
		}
		
		private function disableItems():void
		{
			for (var i:int = 0; i < _mcItems.size; i++) 
			{
				(_mcItems.getItemAt(i) as PhotoCenterComboBoxItem).disable();
				//trace((_mcItems.size - i) / 10);
				TweenMax.delayedCall((_mcItems.size - i) / 50, (_mcItems.getItemAt(i) as PhotoCenterComboBoxItem).transitionOut);
			}
			
			//unblock arrow when animation finishes - last element transitionIn is Complete
			TweenMax.delayedCall((.05 * _mcItems.size) + .3, unblockArrow);
		}
		
		private function blockArrow():void
		{
			_comboArrow.mouseChildren = false;
			_comboArrow.mouseEnabled = false;
		}
		
		private function unblockArrow():void
		{
			_comboArrow.mouseChildren = true;
			_comboArrow.mouseEnabled = true;
		}
		
		override public function validateNow():void
		{
			//remove objects from the container
			cleanContainer();
			
			//create items
			createItems();
			
			//set proper maxHeight property
			maxHeight = itemsContainer.height + 5;
		}
		
		private function createItems():void
		{
			var posY:Number = 0;
			for (var i:int = 0; i < items.size; i++) 
			{
				var item:PhotoCenterComboBoxItem = new PhotoCenterComboBoxItem();
				item.id = _items.getItemAt(i).data;
				item.label = _items.getItemAt(i).label;
				item.mc.bg.width = bg.width - 4;
				item.x = 2;
				item.y = posY;
				item.addEventListener(ButtonEvent.BUTTON_CLICKED, handleButtonClick, false, 0, true);
				_mcItems.addItem(item);
				itemsContainer.addChild(item);
				posY += item.height;
			}
		}
		
		private function handleButtonClick(e:ButtonEvent):void 
		{
			_selectedIndex = _mcItems.indexOf(e.target);	
			selectedItem = _items.getItemAt(_selectedIndex);
			_selectedLabel = selectedItem.label;
			_comboMainLabel.tf.text = _selectedLabel;
			close();
		}
		
		private function cleanContainer():void
		{
			while (itemsContainer.numChildren > 0)
			{
				//remove listeners
				PhotoCenterComboBoxItem(itemsContainer.getChildAt(0)).removeEventListener(ButtonEvent.BUTTON_CLICKED, handleButtonClick);
				itemsContainer.getChildAt(0);
			}
			
			_mcItems.clear();
		}
		
		/**
		 * Sets current comboBox background MovieClip
		 */
		public function set bg(value:MovieClip):void
		{
			if (value) _bg = value;
		}
		
		/**
		 * Returns current comboBox background MovieClip
		 */
		public function get bg():MovieClip
		{
			return _bg;
		}
		
		override public function set defaultText(value:String):void
		{
			super.defaultText = value;
			_comboMainLabel.tf.text = defaultText;
		}
	}
}